<template>
  <div class="sidebar">
    <el-menu :default-active="this.$route.path" class="sidebar-el-menu" :collapse="collapse" background-color="#324157" text-color="#bfcbd9"
             active-text-color="#20a0ff" unique-opened router>
      <el-submenu v-for="item in CTitem" :index="item.index">
        <template slot="title">
          <i :class="item.icon"></i>
          <span slot="title">{{item.title}}</span>
        </template>
        <el-menu-item-group v-for="e in item.subs">
          <!-- <span slot="title">{{}}</span> -->
          <el-menu-item :index="e.index">
            <i :class="e.icon"></i>
            {{e.title}}
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
import bus from '../common/bus';
export default {
  data() {
    return {
      collapse: false,
      CTitem: [],
    };
  },
  props: ["routhData"],
  watch: {
    routhData: function(n, o) {
      if (n) this.routhDataRestructure(n);
    }
  },
  computed: {
    // onRoutes() {
    //   let routhName = this.$route.path;
    //   return routhName;
    // },

  },
  created() {
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on('collapse', msg => {
      this.collapse = msg;
      bus.$emit('collapse-content', msg);
    });
  },
  methods: {
    //路由取值重组
    routhDataRestructure(n) {
      for (let i = 0; i < n.length; i++) {
        this.CTitem.push({
          icon: n[i].icon,
          index: '/' + n[i].url,
          title: n[i].name,
          routh: n[i].childUrl,
          subs: []
        })
        for (let y = 0; y < n[i].childMenus.length; y++) {
          this.CTitem[i].subs.push({
            icon: n[i].childMenus[y].icon,
            index: '/' + n[i].childMenus[y].url,
            title: n[i].childMenus[y].name,
            id: n[i].childMenus[y].id
          })
        }
      }
      bus.$emit('CTitem', this.CTitem);
    },
  }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 190px;
}
.sidebar > ul {
    height: 100%;
}
</style>
